import apiRequest from "@/store/axios";
import { StudentForm, ApiResponse } from "@/types";

// 获取学生列表
const getStudentList = (params?: any) => {
    return apiRequest<StudentForm[]>("/student/list", params, "get");
};

// 添加学生
const addStudent = (params: StudentForm) => {
    return apiRequest<null>("/student/add", params, "post");
};

// 更新学生信息
const updateStudent = (id: string, params: StudentForm) => {
    return apiRequest<null>(`/student/update/${id}`, params, "put");
};

// 删除学生
const deleteStudent = (id: string) => {
    return apiRequest<null>(`/student/delete/${id}`, null, "delete");
};

// 导出学生数据
const exportStudents = () => {
    return apiRequest<Blob>("/student/export", {}, "get", { responseType: 'blob' });
};

// 批量导入学生
const importStudents = (file: File) => {
    const formData = new FormData();
    formData.append('file', file);
    return apiRequest<null>("/student/import", formData, "post", {
        headers: { 'Content-Type': 'multipart/form-data' }
    });
};

// 下载模板
const downloadTemplate = () => {
    return apiRequest<Blob>("/student/template", {}, "get", { responseType: 'blob' });
};

// 预览导入数据
const previewImport = (file: File) => {
    const formData = new FormData();
    formData.append('file', file);
    return apiRequest<any[]>("/student/preview-import", formData, "post", {
        headers: { 'Content-Type': 'multipart/form-data' }
    });
};

// 确认导入
const confirmImport = (data: any[]) => {
    return apiRequest<null>("/student/confirm-import", { data }, "post");
};

export default {
    getStudentList,
    addStudent,
    updateStudent,
    deleteStudent,
    exportStudents,
    importStudents,
    downloadTemplate,
    previewImport,
    confirmImport
};
